﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.Enums;
using Sector7.Medical.EntityType;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;

namespace Sector7.Medical.STOM.Controls
{
    /// <summary>
    /// DrugItemControlForPUR.xaml 的交互逻辑
    /// </summary>
    public partial class DrugItemControlForPUR : UserControl
    {
        #region Accessor
        /// <summary>
        /// 点击添加事件
        /// </summary>
        public event EventHandler ClickAdd = null;

        /// <summary>
        /// 点击删除事件
        /// </summary>
        public event EventHandler ClickDelete = null;

        /// <summary>
        /// 药品项改变事件
        /// </summary>
        public event EventHandler ItemUpdated = null;

        /// <summary>
        /// 获取药品项唯一编号
        /// </summary>
        public long ItemId
        {
            get { return m_uniqueId; }
        }
        #endregion

        #region Public functions
        /// <summary>
        /// 获取控件相关采购单项信息
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public M7_PurchasesBillItem GetItem(long billId)
        {
            M7_PurchasesBillItem item = null;
            try
            {
                item = new M7_PurchasesBillItem();
                var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
                item.BillId = billId;
                item.DrugId = selectedDrug.DrugId;
                item.ItemId = m_uniqueId;

                var spec = specControl.GetSpec();
                item.SpecId = spec.SpecId;
                item.UnitId = spec.PackUnitId;

                item.Number = Convert.ToDecimal(txbNumber.Text);
                item.PurchasesPrice = Convert.ToDecimal(txbPurchasesPrice.Text);
                item.SalesPrice = Convert.ToDecimal(txbSalesPrice.Text);
                item.BatchNumber = txbBatchNumber.Text;
                item.LotNumber = Convert.ToInt32(txbLotNumber.Text);
                item.ProducedDate = (DateTime)dtpProducedDate.SelectedDate;
                item.AvailableDate = (DateTime)dtpAvailibleDate.SelectedDate;
            }
            catch (Exception)
            {
                item = null;
            }

            return item;
        }
        #endregion

        #region Private functions
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            m_uniqueId = DateTime.Now.Ticks;
            if (dtpProducedDate.SelectedDate == null)
            {
                dtpProducedDate.SelectedDate = DateTime.Now;
            }
            if (dtpAvailibleDate.SelectedDate == null)
            {
                dtpAvailibleDate.SelectedDate = DateTime.Now;
            }

            cbbDrugList.TemplateType = DrugTemplateType.PurchasesBill;

            var usageUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.USAGE_UNIT);
            var packUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.PACK_UNIT);
            var stockUnitList = Agent_INFS.GetUnitListByFlags(C_UnitFlags.STOCK_UNIT);
            specControl.InitializeUnitList(usageUnitList, packUnitList, stockUnitList);
        }

        private void cbbDrugList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
            if (selectedDrug != null)
            {
                lblDrugId.Content = selectedDrug.DrugId.ToString();
                lblProductName.Content = selectedDrug.ProductName;
                lblSpec.Content = string.Format("{0} {1} / {2}",
                    selectedDrug.Spec,
                    selectedDrug.SpecUnit.UnitName,
                    selectedDrug.UsageUnit.UnitName);
                specControl.InitializeSpecList(selectedDrug.DrugId, selectedDrug.SpecList);

                //OnItemUpdated(e);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            OnClickAdd(new EventArgs());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            OnClickDelete(new EventArgs());
        }

        private void OnClickAdd(EventArgs e)
        {
            if (ClickAdd != null)
            {
                ClickAdd(this, e);
            }
        }

        private void OnClickDelete(EventArgs e)
        {
            if (ClickDelete != null)
            {
                ClickDelete(this, e);
            }
        }

        private void OnItemUpdated(EventArgs e)
        {
            if (ItemUpdated != null)
            {
                ItemUpdated(this, e);
            }
        }
        #endregion

        #region Structure
        public DrugItemControlForPUR()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private long m_uniqueId = -1;
        #endregion
    }
}
